insheet using "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf_tbar_Ndis.csv", comma clear
keep if choiceset==1
qui count
global n1 = r(N)
keep itidx
duplicates drop
qui count
global n2 = r(N)

insheet using "$tables/teacher_pref_estimates_rc6_totVA_DISAD_Ndis.csv", comma clear

ren (v1 v2) (estimates se)

gen title = ""

replace title = "Constant" if _n==1
replace title = "Commute Time" if _n==2
replace title = "Commute Time Missing" if _n==3
replace title = "Value Added" if _n==4
replace title = "Value Added Mean" if _n==5
replace title = "Commute Time Mean" if _n==6
replace title = "Commute Time Missing Mean" if _n==7
replace title = "Frac. Black" if _n==8
replace title = "Frac. Hispanic" if _n==9
replace title = "N Disadv." if _n==10
replace title = "Frac. Above Med. Achiev." if _n==11
replace title = "Black x Frac. Black" if _n==12
replace title = "Hispanic x Frac. Hispanic" if _n==13
replace title = "Abs Adv x N Disadv." if _n==14
replace title = "Abs Adv x Frac. Hispanic" if _n==15
replace title = "Abs Adv x Frac. Black" if _n==16
replace title = "Abs Adv x Frac. Above Med. Achiev." if _n==17
replace title = "Frac. Black Mean" if _n==18
replace title = "Frac. Hispanic Mean" if _n==19
replace title = "Frac. Above Med. Ach. Mean" if _n==20
replace title = "N. Disadv. Mean" if _n==21
replace title = "Black x Frac. Black Mean" if _n==22
replace title = "Hispanic x Frac. Hispanic Mean" if _n==23
replace title = "Abs Adv x N Disadv. Mean" if _n==24
replace title = "Abs Adv x Frac. Hispanic Mean" if _n==25
replace title = "Abs Adv x Frac. Black Mean" if _n==26
replace title = "Abs Adv x Frac. Above Med. Achiev. Mean" if _n==27
replace title = "Number of Students Mean" if _n==28
replace title = "In District" if _n==29
replace title = "Black" if _n==30
replace title = "Hispanic" if _n==31
replace title = "Female" if _n==32
replace title = "Experience 2-3" if _n==33
replace title = "Experience 4-6" if _n==34
replace title = "Experience 7+" if _n==35
replace title = "VA Non-Disadv. Students" if _n==36
replace title = "VA Disadv. Students" if _n==37
replace title = "St Dev Random Effect" if _n==38
replace title = "St Dev Value Added RC" if _n==39
replace title = "St Dev Frac. Black RC" if _n==40
replace title = "St Dev Frac. Hispanic RC" if _n==41
replace title = "St Dev N. Disadv. RC" if _n==42
replace title = "St Dev Frac. Above Med. Achiev. RC" if _n==43


replace estimate = abs(estimate) if _n>=38

gen new_order = .
replace new_order = 1 if _n==1
replace new_order = 2 if _n==2
replace new_order = 3 if _n==3
replace new_order = 4 if _n==4
replace new_order = 5 if _n==39
replace new_order = 6 if _n==10
replace new_order = 7 if _n==8
replace new_order = 8 if _n==9
replace new_order = 9 if _n==11
replace new_order = 10 if _n==14
replace new_order = 11 if _n==16
replace new_order = 12 if _n==15
replace new_order = 13 if _n==17
replace new_order = 14 if _n==12
replace new_order = 15 if _n==13
replace new_order = 16 if _n==42
replace new_order = 17 if _n==40
replace new_order = 18 if _n==41
replace new_order = 19 if _n==43
replace new_order = 20 if _n==36
replace new_order = 21 if _n==37
replace new_order = 22 if _n==29
replace new_order = 23 if _n==30
replace new_order = 24 if _n==31
replace new_order = 25 if _n==32
replace new_order = 26 if _n==33
replace new_order = 27 if _n==34
replace new_order = 28 if _n==35
replace new_order = 29 if _n==38
replace new_order = 30 if _n==21
replace new_order = 31 if _n==6
replace new_order = 32 if _n==7
replace new_order = 33 if _n==5
replace new_order = 34 if _n==18
replace new_order = 35 if _n==19
replace new_order = 36 if _n==20
replace new_order = 37 if _n==24
replace new_order = 38 if _n==26
replace new_order = 39 if _n==25
replace new_order = 40 if _n==27
replace new_order = 41 if _n==22
replace new_order = 42 if _n==23
replace new_order = 43 if _n==28

sort new_order

forv i=1/43 {
	gen var`i' = .
	local lb = title[`i']
	label var  var`i' "`lb'"
	
	matrix mat`i' = (estimate[`i'],se[`i'])
	matrix rownames mat`i' = var`i'
}

gen label1 = .
mat matlabel1 = (.,.)
label var label1 "\textbf{School Characteristics and Interactions}"
matrix rownames matlabel1 = label1

gen label2 = .
mat matlabel2 = (.,.)
label var label2 "\textbf{Teacher Characteristics}"
matrix rownames matlabel2 = label2

gen label3 = .
mat matlabel3 = (.,.)
label var label3 "\textbf{Chamberlain-Mundlak Device}"
matrix rownames matlabel3 = label3


matrix fullmat = mat1

forv i=2/43 {
	if `i' == 6 {
		matrix fullmat = fullmat\matlabel1
	}
	if `i' == 20 {
		matrix fullmat = fullmat\matlabel2
	}
	if `i' == 30 {
		matrix fullmat = fullmat\matlabel3
	}
	matrix fullmat = fullmat\mat`i'
}

matrix matwide1 = mat1
forv i=2/22 {
	if `i' == 6 {
		matrix matwide1 = matwide1\matlabel1
	}
	if `i' == 20 {
		matrix matwide1 = matwide1\matlabel2
	}
	matrix matwide1 = matwide1\mat`i'
}

matrix matwide2 = mat23
forv i=24/43 {
	if `i' == 30 {
		matrix matwide2 = matwide2\matlabel3
	}
	matrix matwide2 = matwide2\mat`i'
}



frmttable using "$tables/teacher_preference_estimates_wide1", statmat(matwide1) replace va tex fra ///
	ctitles("","Estimate","Standard Error") sdec(3)
	
	local n1 : di %8.0fc $n1
	local n2 : di %8.0fc $n2
frmttable using "$tables/teacher_preference_estimates_wide2", statmat(matwide2) replace va tex fra ///
	ctitles("","Estimate","Standard Error") sdec(3) addrows("\hline\textbf{Sample Size: N Applicants}","`n2'","" \ "\textbf{Sample Size: N Obs}","`n1'","")
	
	
insheet using "$basedata/estimationdata/All_T_J/All_T_J_choices_buff_0_0_lf_principalExtra.csv", comma clear
qui count
global n1 = r(N)
keep jtidx
duplicates drop
qui count
global n2 = r(N)
	
insheet using "$tables/principal_pref_estimates_totVA_extraX.csv", comma clear

ren (v1 v2) (estimates se)

gen title = ""

replace title = "Constant" if _n==1
replace title = "Value Added" if _n==2
replace title = "Value Added x Title I" if _n==3
replace title = "Experience 2-3" if _n==4
replace title = "Experience 2-3 x Title I" if _n==5
replace title = "Experience 4-6" if _n==6
replace title = "Experience 4-6 x Title I" if _n==7
replace title = "Experience 7+" if _n==8
replace title = "Experience 7+ x Title I" if _n==9
replace title = "Experience Missing" if _n==10
replace title = "Experience Missing x Title I" if _n==11
replace title = "Masters" if _n==12
replace title = "Masters x Title I" if _n==13
replace title = "Black" if _n==14
replace title = "Black x Title I" if _n==15
replace title = "Black x Frac. Black" if _n==16
replace title = "Black x Frac. Black x Title I" if _n==17
replace title = "Hispanic" if _n==18
replace title = "Hispanic x Title I" if _n==19
replace title = "Hispanic x Frac. Hispanic" if _n==20
replace title = "Hispanic x Frac. Hispanic x Title I" if _n==21
replace title = "Female" if _n==22
replace title = "Female x Title I" if _n==23
replace title = "Gender Missing" if _n==24
replace title = "Gender Missing x Title I" if _n==25
replace title = "Race Missing" if _n==26
replace title = "Race Missing x Title I" if _n==27
replace title = "VA Missing" if _n==28
replace title = "VA Missing x Title I" if _n==29
replace title = "Title I" if _n==30
replace title = "Praxis" if _n==31
replace title = "Praxis x Title I" if _n==32
replace title = "Praxis Missing" if _n==33
replace title = "Praxis Missing x Title I" if _n==34
replace title = "Grad Deg" if _n==35
replace title = "Grad Deg x Title I" if _n==36
replace title = "Grad Deg Missing" if _n==37
replace title = "Grad Deg Missing x Title I" if _n==38
replace title = "Certified" if _n==39
replace title = "Certified x Title I" if _n==40
replace title = "Certified Missing" if _n==41
replace title = "Certified Missing x Title I" if _n==42
replace title = "Licensed" if _n==43
replace title = "Licensed x Title I" if _n==44
replace title = "Licensed Missing" if _n==45
replace title = "Licensed Missing x Title I" if _n==46

replace title = "St Dev Random Effect" if _n==47

replace estimate = abs(estimate) if _n>=47


gen new_order = .
replace new_order = 1 if _n==1
replace new_order = 2 if _n==47
replace new_order = 3 if _n==30
replace new_order = _n+2 if _n>=2 & _n<30
replace new_order = _n+1 if _n>=31 & _n<47

sort new_order

forv i=1/45 {
	gen var`i' = .
	local lb = title[`i']
	label var  var`i' "`lb'"
	
	matrix mat`i' = (estimate[`i'],se[`i'])
	matrix rownames mat`i' = var`i'
}



matrix fullmat = mat1

forv i=2/45 {
	matrix fullmat = fullmat\mat`i'
}

gen label1 = .
mat matlabel1 = (.,.)
label var label1 "\textbf{Characteristics}"
matrix rownames matlabel1 = label1

matrix matwidep1 = matlabel1
forv i=1/23 {
	matrix matwidep1 = matwidep1\mat`i'
}

matrix matwidep2 = mat24
forv i=25/45 {
	matrix matwidep2 = matwidep2\mat`i'
}


frmttable using "$tables/principal_preference_estimates_wide1", statmat(matwidep1) replace va tex fra ///
	ctitles("","Estimate","Standard Error") sdec(3)
	
	local n1 : di %8.0fc $n1
	local n2 : di %8.0fc $n2
	
frmttable using "$tables/principal_preference_estimates_wide2", statmat(matwidep2) replace va tex fra ///
	ctitles("","Estimate","Standard Error") sdec(3) addrows("\hline\textbf{Sample Size: N Positions}","`n2'","" \ "\textbf{Sample Size: N Obs}","`n1'","")
